package com.dm0858.bianmin.mine.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dm0858.bianmin.app.MyApp;
import com.dm0858.bianmin.mine.bean.CarBeanData;
import com.dm0858.bianmin.mine.bean.HouseBeanData;
import com.dm0858.bianmin.mine.bean.MyFavoriteBeanData;
import com.dm0858.bianmin.mine.bean.NormalBeanData;
import com.dm0858.bianmin.mine.bean.UserInfoBeanData;
import com.dm0858.bianmin.mine.listener.IResultListener;
import com.dm0858.bianmin.mine.listener.IWXLoginListener;
import com.dm0858.bianmin.mine.listener.IWXWithPhoneListener;
import com.dm0858.bianmin.mine.model.MineModel;
import com.dm0858.bianmin.mine.network.Retrofit;
import com.dm0858.bianmin.mine.network.RetrofitListener;
import com.dm0858.bianmin.mine.tools.LogUtils;
import com.dm0858.bianmin.mine.tools.SPUtils;
import com.dm0858.bianmin.mine.tools.ToastUtils;

import java.util.List;

public class MineViewModel extends ViewModel {

    private Retrofit retrofit;

    private SPUtils sp = SPUtils.getInstance("user_info");
    //头像
    private MutableLiveData<String> avatar;
    //昵称
    private MutableLiveData<String> nickName;
    //openid
    private MutableLiveData<String> openid;
    //access_token
    private MutableLiveData<String> accessToken;

    private MutableLiveData<NormalBeanData> normal;

    private MutableLiveData<HouseBeanData> house;

    private MutableLiveData<CarBeanData> car;

    private MutableLiveData<List<MyFavoriteBeanData.DataBean>> myFavoriteList;


    public MineViewModel() {
        if (retrofit == null) retrofit = new Retrofit();
    }

    public MutableLiveData<String> getAvatar() {
        if (avatar == null) {
            avatar = new MutableLiveData<>();
            avatar.setValue(sp.getString("avatar",""));
        }
        return avatar;
    }

    public void setAvatar(String avatar) {
        sp.put("avatar",avatar,true);
        getAvatar().setValue(avatar);
    }

    public MutableLiveData<String> getNickName() {
        if (nickName == null) {
            nickName = new MutableLiveData<>();
            nickName.setValue(sp.getString("nickName","账号/微信登录"));
        }
        return nickName;
    }

    public void setNickName(String nickName) {
        sp.put("nickName",nickName,true);
        getNickName().setValue(nickName);
    }

    public MutableLiveData<String> getOpenid() {
        if (openid == null) {
            openid = new MutableLiveData<>();
            openid.setValue(sp.getString("openid",""));
        }
        return openid;
    }

    public void setOpenid(String openid) {
        sp.put("openid",openid,true);
        getOpenid().setValue(openid);
    }

    public MutableLiveData<String> getAccessToken() {
        if (accessToken == null) {
            accessToken = new MutableLiveData<>();
            accessToken.setValue(sp.getString("accessToken",""));
        }
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        sp.put("accessToken",accessToken,true);
        getAccessToken().setValue(accessToken);
    }

    public MutableLiveData<NormalBeanData> getNormal() {
        if (normal == null) {
            normal = new MutableLiveData<>();
            normal.setValue(null);
        }
        return normal;
    }

    public void setNormal(NormalBeanData normal) {
        getNormal().setValue(normal);
    }

    public MutableLiveData<HouseBeanData> getHouse() {
        if (house == null) {
            house = new MutableLiveData<>();
            house.setValue(null);
        }
        return house;
    }

    public void setHouse(HouseBeanData house) {
        getHouse().setValue(house);
    }

    public MutableLiveData<CarBeanData> getCar() {
        if (car == null) {
            car = new MutableLiveData<>();
            car.setValue(null);
        }
        return car;
    }

    public void setCar(CarBeanData car) {
        getCar().setValue(car);
    }

    public MutableLiveData<List<MyFavoriteBeanData.DataBean>> getMyFavoriteList() {
        if (myFavoriteList == null) {
            myFavoriteList = new MutableLiveData<>();
            myFavoriteList.setValue(null);
        }
        return myFavoriteList;
    }

    public void setMyFavoriteList(List<MyFavoriteBeanData.DataBean> myFavoriteList) {
        getMyFavoriteList().setValue(myFavoriteList);
    }


    /**
     *
     * @description 微信登录成功后上传信息
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public void wxLogin(String openid, String nickname, String unionid, String headimgurl, IWXLoginListener listener) {
        MineModel.wxLogin(retrofit,openid,nickname,unionid,headimgurl,listener);
    }

    /**
     *
     * @description 获取验证码
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public void getCode(String phone,IResultListener listener) {
        MineModel.sendPhoneCode(retrofit,phone,listener);
    }

    /**
     *
     * @description 绑定手机号
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public void wxWithPhone(String openid, String phone, String code, IWXWithPhoneListener listener) {
        MineModel.wxWithPhone(retrofit,openid,phone,code,listener);
    }

    /**
     *
     * @description 获取用户的信息
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public void userInfo() {
        if (!MyApp.userid.equals("")){
            MineModel.userInfo(retrofit, MyApp.userid, new RetrofitListener() {
                @Override
                public void onSuccess(Object object) {
                    UserInfoBeanData beanData = JSONObject.parseObject(object.toString(),UserInfoBeanData.class);
                    setAvatar(beanData.getHeadimgurl());
                    setNickName(beanData.getNickname());
                    MyApp.openid = beanData.getOpenid();
                    MyApp.userid = beanData.getId();
                    MyApp.phone = beanData.getPhone();
                    SPUtils.getInstance("user_info").put("userid",MyApp.userid,true);
                    SPUtils.getInstance("user_info").put("phone",MyApp.phone,true);
                    SPUtils.getInstance("user_info").put("openid",MyApp.openid,true);
                }

                @Override
                public void onError(int messageCode, String e) {
                    ToastUtils.showShort(e);
                }
            });
        }
    }

    /**
     *
     * @description 修改帖子
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-20
     **/
    public void editPost(int typeInt,String id) {
        MineModel.editPost(retrofit, id, new RetrofitListener() {
            @Override
            public void onSuccess(Object object) {
                switch (typeInt) {
                    case 0:
                        NormalBeanData normal = JSON.parseObject(object.toString(),NormalBeanData.class);
                        setNormal(normal);
                        break;
                    case 10:
                        HouseBeanData house = JSON.parseObject(object.toString(),HouseBeanData.class);
                        setHouse(house);
                        break;
                    case 17:
                        LogUtils.d(object.toString());
                        CarBeanData car = JSON.parseObject(object.toString(),CarBeanData.class);
                        setCar(car);
                        break;
                }
            }

            @Override
            public void onError(int messageCode, String e) {

            }
        });
    }

    /**
     * 
     * @description 我的收藏
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-20
     **/
    public void userCollectArticle() {
        MineModel.userCollectArticle(retrofit, MyApp.userid, object -> {
            setMyFavoriteList((List<MyFavoriteBeanData.DataBean>) object);
        });
    }

}
